
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import '../../entity/reader_entity.dart';
import '../../http/api.dart';
import '../../main.dart';

class ListModel{

  final RxList<ReaderCurrentBorrowList> _list = RxList();
  final _allList = <ReaderCurrentBorrowList>[];
  bool showButton = false;
  String filter = '';


  filterList(filter){
    this.filter = filter;
    _list.clear();
    if(filter.isEmpty){
      _list.addAll(_allList);
      return;
    }
    for(var element in _allList) {
      if (element.toString().contains(filter)) {
        _list.add(element);
      }
    }
  }

  Widget show(list,{bool showButton = false, String filter = ''}){
    this.showButton = showButton;
    this.filter = filter;
    _list.clear();
    _allList.clear();
    _list.addAll(list);
    _allList.addAll(list);
    return  Obx(() => Expanded(child: ListView.builder(
      itemBuilder: (context, index) {
        return _itemBuilder(index);
      },
      itemCount: _list.length,
    )));
  }

  _itemBuilder(index){
    var item = _list[index];
    return Container(
        padding: EdgeInsets.only(top: 16.h,bottom: 16.h,left: 26.w,right: 26.w),
        decoration: const BoxDecoration(
          image: DecorationImage(
            fit: BoxFit.fill,
            image: AssetImage(
              "assets/images/borrow_item_bg.png",
            ),
          ),
        ),
        // alignment: Alignment.center,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                Image.asset("assets/images/item_icon.png",width: 10.w,fit: BoxFit.fitWidth,),
                SizedBox(width: 282.w,child: Text('《${item.bookTitle}》',style: TextStyle(color: Colors.blueAccent,fontSize: 10.sp)),),
                Image.asset("assets/images/${item.overdue!?'out':'normal'}.png",width: 60.w,fit: BoxFit.fitWidth,),
                // if(item.overdue)...[
                //   Text('续借成功',style: TextStyle(color:  const Color.fromRGBO(19, 136, 8, 1),fontSize: 10.sp))
                // ]
              ],
            ),
            SizedBox(height: 5.h,),
            const Divider(height: 1.0,color: Colors.grey,),
            SizedBox(height: 10.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('图书编号：',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
                Text('${item.bookNo}',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),

              ],
            ),
            SizedBox(height: 5.h,),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Text('应还时间：',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
                Text('${item.readyReturnDate}',style: TextStyle(color: Colors.black54,fontSize: 8.sp)),
              ],
            ),
            SizedBox(height: 5.h,),
            if(!showButton)...[
              SizedBox(height: 5.h,),
            ],
            if(showButton)...[
              Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Container(height:30.h,alignment: Alignment.center,child: Text(item.error??'',
                    style: TextStyle(color: item.status==1?Colors.red:const Color.fromRGBO(19, 136, 8, 1),fontSize: 8.sp),textAlign: TextAlign.center,),),
                  if(item.status==0)...[
                    InkWell(
                        onTap: () async {
                          var result = await bookBorrowAgain(currentTask.uid,item.bookNo);
                          if(result.$1 == 200){
                            tts.speak('续借成功');
                            item.error = '续借成功';
                            item.status = 2;
                            item.readyReturnDate = result.$2.readyReturnDate;
                            _list.removeAt(index);
                            _list.insert(index, item);
                          }else{
                            tts.speak('续借失败');
                            item.error = result.$2;
                            item.status = 1;
                            _list.removeAt(index);
                            _list.insert(index, item);
                          }
                        },
                        child: Align(alignment: Alignment.centerRight,child: Image.asset("assets/images/borrow_again.png",width: 60.w,fit: BoxFit.fitWidth,),)
                    )
                  ]
                ],
              )
            ],
          ],
        )
    );
  }
}

